import axios from "axios";
import qs from "qs";
import { Message } from "element-ui";
import that from "../main";
import { Loading } from "element-ui";

// 如果接口500ms内返回，不要遮罩层；否则添加遮罩层
let loadingInstance = null; //蒙版实例
let timer = null; //定时器

const $http = axios.create({
    // baseURL: "http://192.168.1.85:5220", //本地
    baseURL: "http://101.43.178.84:5220", //外网
    timeout: 1800000,
    responseType: "json",
    headers: {
        "Content-Type": "application/x-www-form-urlencoded"
    }
});

// 请求qs序列化
$http.interceptors.request.use(
    config => {
        // 添加遮罩层
        if (timer !== null) {
            clearTimeout(timer);
        }
        timer = setTimeout(() => {
            //如果存在则关闭
            if (loadingInstance !== null) {
                loadingInstance.close();
            }
            loadingInstance = Loading.service({
                lock: true,
                text: "努力加载中！",
                spinner: "el-icon-loading",
                background: "rgba(0, 0, 0, 0.7)"
            });
        }, 300);
        const token = sessionStorage.getItem("token");
        // 此处添加sys_flag：I根据不同内外网切换
        config.data = { ...config.data, sys_flag: "I" };
        //config.data = { ...config.data, sys_flag: "0" };//外网
        if (
            config.method === "post" &&
            config.headers["Content-Type"] ===
                "application/x-www-form-urlencoded"
        ) {
            config.data = qs.stringify(config.data, {
                arrayFormat: "indices",
                allowDots: true
            });
        }

        if (token) {
            config.headers.token = token;
        }
        return config;
    },
    error => {
        Message({
            showClose: true,
            message: error && error.data.error_msg,
            type: "error"
        });
        return Promise.reject(error.data.error_msg);
    }
);

// 接收数据，刷新token，封装res.bus_json
$http.interceptors.response.use(
    res => {
        //如果请求返回快的话，就不需要loading
        // 清除定时器
        clearTimeout(timer);
        timer = null;
        // 请求返回后，关闭loading
        if (loadingInstance !== null) {
            loadingInstance.close();
        }
        if (res.data) {
            if (res.data.token && res.data.token.length) {
                sessionStorage.setItem("token", res.data.token);
            }

            switch (res.data.error_code) {
                case "-1":
                    Message.warning({
                        showClose: true,
                        message: "系统安全保护，请重新登录!"
                    });
                    sessionStorage.clear();
                    setTimeout(() => {
                        that.$router.push({
                            path: "/login"
                        });
                    }, 1000);
                    break;
                case "0":
                    return res.data;
                case "1":
                    Message.error({
                        showClose: true,
                        message: res.data.error_msg
                    });
                    break;
                case "2":
                    Message.warning({
                        showClose: true,
                        message:
                            "您没有登录权限，请重新登录确认，正在跳转到登录页面"
                    });
                    sessionStorage.clear();
                    setTimeout(() => {
                        that.$router.push({
                            path: "/login"
                        });
                    }, 1000);
                    break;
                default:
                    //其他默认返回的blob文件流

                    return res.data;
            }
        } else {
            return res;
        }
    },
    error => {
        //如果请求返回快的话，就不需要loading
        // 清除定时器
        clearTimeout(timer);
        timer = null;
        // 请求返回后，关闭loading
        if (loadingInstance !== null) {
            loadingInstance.close();
        }
        console.log(error);
    }
);

export default $http;
